<template>
  <el-dialog title="批量审批" :model-value="visible" @close="onClose" :width="600">
    <el-form ref="form" size="small" label-width="80px" :rules="rules">
      <el-form-item :label="'审批意见'">
        <el-input v-model="comment" type="textarea"></el-input>
      </el-form-item>
      <el-form-item>
        <div class="form_button">
          <div class="form_button_item" v-for="(item, key) in buttonList" :key="key">
            <el-button size="small" :type="item.type" plain @click="Task(item.key, item.title)">{{
              item.title
            }}</el-button>
          </div>
        </div>
      </el-form-item>
    </el-form>
  </el-dialog>
  <Complaint @submit="submit" :info="info" v-model:modelVisible="modelVisible"></Complaint>
</template>
<script lang="ts">
import { defineComponent, ref, reactive, inject } from 'vue'
import { ElMessage } from 'element-plus'
import Complaint from './Complaint.vue'
import { iBatchThrough,iBatchRejected } from './utils/api'
// import { ObjectType } from 'typings/common'

// import useVModel from '@/hooks/useVModel'
export default defineComponent({
  components: {
    Complaint,
  },

  emits: ['update:visible'],
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    examineAndApprove: {
      type: Object,
      required: true,
    },
  },
  setup(props, { emit }) {
    const parent = inject('parent', Function, true)
    const info = ref<any>({})
    const comment = ref<any>('')
    const modelVisible = ref<boolean>(false)
    const buttonList = reactive<any>([
      { type: 'success', title: '通过', key: 1 },
      { type: 'danger', title: '驳回', key: 2 },
    ])
    // 确定
    const submit = async (key) => {
      let rst: any = null
      switch (key.key) {
        case 1:
          rst = await iBatchThrough({
            comment: comment.value,
            processInstanceId: props.examineAndApprove.map((item) => item.approvalNo),
          })
          break
        case 2:
          rst = await iBatchRejected({
            comment: comment.value,
            processInstanceId: props.examineAndApprove.map((item) => item.approvalNo),
          })
          break
      }
      if (rst.code === 200) {
        ElMessage.success(info.value.title + '处理成功')
        modelVisible.value = false
        emit('update:visible', false)
        parent(rst)
      } else {
        ElMessage.error('办理失败，请联系管理员')
      }
    }
    //
    const Task = (key, title) => {
      info.value = {
        key,
        title,
      }
      modelVisible.value = true
    }
    // 关闭
    const onClose = () => {
      emit('update:visible', false)
    }
    return {
      comment,
      submit,
      modelVisible,
      Task,
      onClose,
      buttonList,
      info,
    }
  },
})
</script>
<style scoped lang="scss">
.form_button {
  display: flex;
  justify-content: flex-start;
  & > div > button {
    margin-right: 10px;
  }
}
</style>
